﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAO;
using System.Data.SqlClient;
using System.Data;
using EOL;
namespace BPO
{
    public class Security
    {
        private static Security instance;

        public static Security getInstance()
        {
            if (instance == null)
                instance = new Security();

            return instance;
        }
        private UserDetail _usr = null;
        public UserDetail UserDetails { get { return _usr; } }
        private Security()
        {

        }
        private string _userRole;
        public bool IsUserLoggedIn { get; set; }
        public UserDetail ValidateUser(string userName, string password, string roleId)
        {
            DataSet ds = null;
            IsUserLoggedIn = false;
            _usr = new UserDetail();
            List<SqlParameter> pList = new List<SqlParameter>();
            DAO.DAO d = new DAO.DAO();
            pList.Add(d.AddParameter("@UserName", userName));
            pList.Add(d.AddParameter("@Password", password));
            pList.Add(d.AddParameter("@RoleId", roleId));
            ds = d.ExecuteDataSet("P_ValidateUser", pList);
            if (ds != null && ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 1)
            {
                _usr.UserId = Convert.ToInt32(ds.Tables[0].Rows[0]["UserId"]);
                _usr.UserName = Convert.ToString(ds.Tables[0].Rows[0]["UserName"]);
                _usr.Password = Convert.ToString(ds.Tables[0].Rows[0]["Password"]);
                _usr.FullName = Convert.ToString(ds.Tables[0].Rows[0]["FullName"]);
                _usr.RoleName = Convert.ToString(ds.Tables[0].Rows[0]["RoleName"]);
                _usr.RoleId = Convert.ToString(ds.Tables[0].Rows[0]["RoleId"]);
                _usr.RankCode = Convert.ToInt32(ds.Tables[0].Rows[0]["RankCode"]);
                IsUserLoggedIn = true;
                _userRole = _usr.RoleId;
            }
            return _usr;
        }

        public string UserRole { get { return _userRole; } }
    }
    public class UserDetail
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string FullName { get; set; }
        public string RoleName { get; set; }
        public string RoleId { get; set; }
        public int RankCode { get; set; }
    }
    public class BusinessRules
    {

        public void SaveEmployeeDetails(Employee objEmp)
        {
            object empId = null;
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@EmployeeName", objEmp.EmployeeName));
            param.Add(d.AddParameter("@FatherName", objEmp.FatherName));
            param.Add(d.AddParameter("@MotherName", objEmp.MotherName));
            param.Add(d.AddParameter("@Qualification1", objEmp.Qualification1));
            param.Add(d.AddParameter("@Qualification2", objEmp.Qualification2));
            param.Add(d.AddParameter("@EmployeeAddress", objEmp.EmployeeAddress));
            param.Add(d.AddParameter("@Age", objEmp.Age));
            param.Add(d.AddParameter("@JoiningDate", objEmp.JoiningDate));
            param.Add(d.AddParameter("@Experience", objEmp.Experience));
            param.Add(d.AddParameter("@Designation", objEmp.Designation));
            param.Add(d.AddParameter("@Salary", objEmp.Salary));
            param.Add(d.AddParameter("@PreviousCompany", objEmp.PreviousCompany));
            param.Add(d.AddParameter("@EmployeeId", objEmp.EmployeeId, SqlDbType.Int, 10, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AddEmployee", param, ref empId);
            objEmp.EmployeeId = Convert.ToInt32(empId);

        }

        public void SaveCustomerDetails(Customer customer)
        {
            object custId = null;
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@CustomerName", customer.CustomerName));
            param.Add(d.AddParameter("@CBranch", customer.CBranch));
            param.Add(d.AddParameter("@CDate", customer.CDate));
            param.Add(d.AddParameter("@CAddress", customer.CAddress));
            param.Add(d.AddParameter("@GuardianName", customer.GuardianName));
            param.Add(d.AddParameter("@CAge", customer.CAge));
            param.Add(d.AddParameter("@AgentId", customer.AgentId));
            param.Add(d.AddParameter("@PlanId", customer.PlanId));
            param.Add(d.AddParameter("@Term", customer.Term));
            param.Add(d.AddParameter("@CNomineeName", customer.CNomineeName));
            param.Add(d.AddParameter("@CNGuardianName", customer.CNGuardianName));
            param.Add(d.AddParameter("@CNAddress", customer.CNAddress));
            param.Add(d.AddParameter("@CNAge", customer.CNAge));
            param.Add(d.AddParameter("@RelationKey", customer.RelationKey));
            param.Add(d.AddParameter("@Amount", customer.Amount));
            param.Add(d.AddParameter("@CustomerId", customer.CustomerId, SqlDbType.Int, 10, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AddCustomer", param, ref custId);
            customer.CustomerId = Convert.ToInt32(custId);


        }


        public void SaveAgent(Agent agent)
        {
            object agentId = null;
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@AgentName", agent.AgentName));
            param.Add(d.AddParameter("@ABranch", agent.ABranch));
            param.Add(d.AddParameter("@ADate", agent.ADate));
            param.Add(d.AddParameter("@AAddress", agent.AAddress));
            param.Add(d.AddParameter("@AGuardianName", agent.AGuardianName));
            param.Add(d.AddParameter("@AAge", agent.AAge));
            param.Add(d.AddParameter("@RankId", agent.RankId));
            param.Add(d.AddParameter("@ANomineeName", agent.ANomineeName));
            param.Add(d.AddParameter("@ANGuardianName", agent.ANGuardianName));
            param.Add(d.AddParameter("@ANAddress", agent.ANAddress));
            param.Add(d.AddParameter("@ANAge", agent.ANAge));
            param.Add(d.AddParameter("@ParentId", agent.ParentId));
            param.Add(d.AddParameter("@AgentId", agent.AgentId, SqlDbType.Int, 10, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AddAgent", param, ref agentId);
            agent.AgentId = Convert.ToInt32(agentId);
        }

        public static int GetRank(int agentId)
        {
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@AgentId", agentId));
            return Convert.ToInt32(d.ExecuteScalar("P_GetAgentRank", param));

        }

        public static List<Employee> GetEmployeeData(string name, DateTime regDate,bool isList)
        {
            DataTable dt = new DataTable();
            List<Employee> lstEmp = new List<Employee>();
            Employee emp = null;
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@EmpName", name));
            param.Add(d.AddParameter("@RegDate", regDate));
            dt= d.ExecuteDataSet("P_GetEmployee", param).Tables[0];
            foreach (DataRow  item in dt.Rows)
            {
                emp = new Employee();
                emp.EmployeeId = Convert.ToInt32(item["EmployeeId"]);
                emp.EmployeeName = Convert.ToString(item["EmployeeName"]);
                emp.FatherName = Convert.ToString(item["FatherName"]);
                emp.MotherName = Convert.ToString(item["MotherName"]);
                emp.Age = Convert.ToInt32(item["Age"]);
                emp.Qualification1 = Convert.ToString(item["Qualification1"]);
                emp.Qualification2 = Convert.ToString(item["Qualification2"]);
                emp.EmployeeAddress = Convert.ToString(item["Address"]);
                emp.Experience = Convert.ToInt32(item["Experience"]);
                emp.Salary = Convert.ToInt64(item["Salary"]);
                emp.Designation = Convert.ToString(item["Designation"]);
                emp.PreviousCompany = Convert.ToString(item["PrevCompany"]);
                emp.JoiningDate = Convert.ToDateTime(item["Joiningdate"]);
                lstEmp.Add(emp);
              
                
            }
            return lstEmp;
        }
        public static DataTable GetCustomerData(string name, DateTime regDate)
        {
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@CustName", name));
            param.Add(d.AddParameter("@RegDate", regDate));
            return d.ExecuteDataSet("P_GetCustomer", param).Tables[0];
        }
        public static DataTable GetAgentData(string name, DateTime regDate)
        {
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@AgentName", name));
            param.Add(d.AddParameter("@RegDate", regDate));
            return d.ExecuteDataSet("P_GetAgent", param).Tables[0];
        }
        public static DataTable GetEmployeeData(string name, DateTime regDate)
        {
            DAO.DAO d = new DAO.DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@EmpName", name));
            param.Add(d.AddParameter("@RegDate", regDate));
            return d.ExecuteDataSet("P_GetEmployee", param).Tables[0];
        }
    }

}
